package com.atguigu.tingshu.account.api;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.login.MyLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.vo.account.AccountDeductVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Tag(name = "用户账户管理")
@RestController
@RequestMapping("api/account/userAccount")
@SuppressWarnings({"all"})
public class UserAccountApiController {

    @Autowired
    private UserAccountService userAccountService;

    //登录之后，远程调用：进行账户初始化
    @GetMapping("initUserAccount/{userId}")
    public Result initUserAccount(@PathVariable Long userId) {
        userAccountService.initUserAccount(userId);
        return Result.ok();
    }

    @MyLogin
    @Operation(summary = "获取账户可用金额")
    @GetMapping("getAvailableAmount")
    public Result<BigDecimal> getAvailableAmount() {
        return Result.ok(userAccountService.getAvailableAmount(AuthContextHolder.getUserId()));
    }

    @PostMapping("checkAndDeduct")
    public Result checkAndDeduct(@RequestBody AccountDeductVo accountDeductVo){
        userAccountService.checkAndDeduct(accountDeductVo);
        return Result.ok();
    }
}

